package org.firefly.util;

public class GeoUtil {

    private static final double EARTH_RADIUS = 6371000; // 地球半径(米)

    /**
     * 使用Haversine公式计算两点间距离
     *
     * @param lat1 点1纬度
     * @param lon1 点1经度
     * @param lat2 点2纬度
     * @param lon2 点2经度
     * @return 距离（米）
     */
    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double latDistance = Math.toRadians(lat2 - lat1);
        double lonDistance = Math.toRadians(lon2 - lon1);

        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
                * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return EARTH_RADIUS * c;
    }

    /**
     * 快速近似距离计算（适用于非关键场景）
     */
    public static double fastApproximateDistance(double lat1, double lon1, double lat2, double lon2) {
        double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2 * Math.PI / 180);
        double y = lat2 - lat1;
        return Math.sqrt(x*x + y*y) * 111319.9; // 每度的近似米数
    }
}